package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.ChapterAndVideoVo.ChapterVo;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/eduservice/chapter")
@Api(description = "章节和小节")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    //根据course_id获取课程章节和小节
    @GetMapping("getChapterVideo/{courseId}")
    public  R getChapterVideo(@PathVariable String courseId){
       List<ChapterVo> ChapterList = eduChapterService.getAllChapterAndVideo(courseId);

        return R.ok().data("list",ChapterList);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok().data("chapter",eduChapter);
    }

    //根据章节Id查询 章节修改需要 先要数据进行回显
    @GetMapping("getChapterById/{chapterId}")
    public R getChapterById(@PathVariable String chapterId){
        EduChapter chapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }

//    //修改章节
//    @PutMapping("updateChapter/{id}")
//    public R updateChapter(
//            @ApiParam(name = "id", value = "章节ID", required = true)
//            @PathVariable String id,
//            @ApiParam(name = "chapter", value = "章节对象", required = true)
//            @RequestBody EduChapter chapter
//
//    ){
//        chapter.setId(id);
//        boolean flag = eduChapterService.updateById(chapter);
//        if (flag){
//            return R.ok();
//        }else {
//            return R.error();
//        }
//    }
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除章节
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag = eduChapterService.deleteChapterById(chapterId);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }



}

